<?php
namespace app\blogadmin\controller;

use app\blogadmin\model\Admin as adminM;
use think\Db;
use think\Request;

class Admin extends Base{
    /**
     * 管理员列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lst(){
        $res=Db::table('admin')->alias("a")->join("admin_role r","a.role_id=r.id",'left') ->order("a.id  ASC")->field("a.id,a.username,a.role_id,r.name,a.phone,a.email,a.last_ip,a.login_time,a.status")->paginate(10);
        $page=$res->render();
        $this->assign('admins',$res);
        $this->assign('page',$page);
        return $this->fetch();
    }


    /**
     * 添加管理员
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function add(){
        //获取管理员角色列表
        $role=Db::table('admin_role')->select();
        if(Request::instance()->isPost()){
            //获取提交的数据，实体化数据
            $data=input("post.");
            $info['username']=htmlspecialchars($data['username']);
            $info['password']=md5(htmlspecialchars($data['password']));
            $info['phone']=htmlspecialchars($data['phone']);
            $info['email']=htmlspecialchars($data['email']);
            $info['role_id']=htmlspecialchars($data['role_id']);
            $info['status']=$data['status'];

            $adminM=new adminM();   //实例化模型
            $check_username=$adminM->getOne(['username'=>$info['username']]);
            if(!empty($check_username))//判断该用户是否存在
            {
                return json(['msg'=>'该管理员已经存在，请重新输入','code'=>'-1']);
            }
            else{
                $res=$adminM->add($info);
                if($res){
                    return json(['msg'=>'添加数据成功','code'=>'1']);
                }else{
                    $this->error('');
                    return json(['msg'=>'添加数据失败','code'=>'-1']);
                }
            }
        }
        $this->assign('role',$role);
        return $this->fetch();
    }

    /**
     * 修改管理员
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(){
       $id=Request::instance()->param('id');//获取用户id
       $adminM=new  adminM();
       $cur_admin=$adminM->getOne(['id'=>$id]); //获取当前用户信息
        //获取管理员角色列表
        $role=Db::table('admin_role')->select();

       if(Request::instance()->isPost()){
            $data=Request::instance()->param();
            $up_admin['username']=htmlspecialchars($data['username']);
            $up_admin['password']=md5(htmlspecialchars($data['password']));
            $up_admin['role_id']=htmlspecialchars($data['role_id']);
            $up_admin['phone']=htmlspecialchars($data['phone']);
            $up_admin['email']=htmlspecialchars($data['email']);
           $up_admin['status']=htmlspecialchars($data['status']);
            $up_admin['id']=$data['id'];

            $res=$adminM->upda($up_admin);
            if($res){
                return json(['code'=>1,'msg'=>"修改数据成功"]);
            }else{
                return json(['code'=>-1,'msg'=>"修改数据失败"]);
            }
       }
       $this->assign('cur_admin',$cur_admin);
        $this->assign('role',$role);
       return $this->fetch();
    }


    /**
     * 删除管理员
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function del(){
        if(Request::instance()->isPost()){
            $id=input('post.id');
            $adminM=new adminM();
            $res=$adminM->del($id);
            if($res) {
                return json(['code'=>'1','msg'=>'删除数据成功']);
            }else{
                return json(['code'=>'0','msg'=>'删除数据失败']);
            }
        }
    }





    public function sss(){
        return $this->fetch();
    }



}